/*
 * Copyright (c) 2014, marco.tamburelli@gmail.com
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met: 
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.omorphdb.core.io;

import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;

/**
 * It represents a read only page. The page is obtained by wrapping a
 * {@link MappedByteBuffer}.
 * 
 * The page can only be read.
 * 
 * @author Marco Tamburelli
 */
abstract class ReadPage extends AbstractPage
{
	private MappedByteBuffer page;

	ReadPage(FileChannel channel, int pageSize) throws IOException
	{
		super(channel, 0, pageSize);

		this.page = super.channel.map(MapMode.READ_ONLY, super.pagePosition, super.pageSize);
	}

	@Override
	long getCursorPosition()
	{
		return this.page.position() + this.pagePosition;
	}

	/**
	 * Provides a new position of the cursor within the file. <br>
	 * The cursor is the place where the next read/write operation will take
	 * effect.
	 * 
	 * @param position
	 * @throws IOException
	 */
	void setCursorPosition(long position) throws IOException
	{
		if (position < super.pagePosition)
		{
			long p = super.pagePosition;

			while (position < p)
			{
				p -= super.pageSize;
			}

			super.pagePosition = p;
			this.page = super.channel.map(MapMode.READ_ONLY, super.pagePosition, super.pageSize);
		}

		if (position >= super.pagePosition + super.pageSize)
		{
			long p = super.pagePosition;

			while (position >= p + super.pageSize)
			{
				p += super.pageSize;
			}

			super.pagePosition = p;
			this.page = super.channel.map(MapMode.READ_ONLY, super.pagePosition, super.pageSize);
		}

		this.page.position((int) (position - super.pagePosition));
	}

	/**
	 * Moves the cursor position of a provided amount. The provided amount can
	 * be negative.
	 * 
	 * @param shift
	 * @return The new cursor position
	 * @throws IOException
	 */
	long moveCursor(int shift) throws IOException
	{
		// FIXME A more efficient way should be found
		long position = this.page.position() + shift;
		setCursorPosition(position);

		return position;
	}

	/**
	 * Fills the target bytes array with bytes read from the number starting at
	 * the provided position. The method returns the count of bytes wrote.
	 * 
	 * @param bytes The byte array which is filled with bytes read from the
	 *        buffer.
	 * @param offset
	 * 
	 * @return
	 */
	private int get(byte[] bytes, int offset)
	{
		int available = this.pageSize - this.page.position();
		int l = bytes.length;

		if (l > available) l = available;

		for (int i = offset; i < l; i++)
			bytes[i] = this.page.get();

		return l;
	}

	/**
	 * Fills the target bytes array with bytes read from the number starting at
	 * the provided position. The method returns the count of bytes wrote.
	 * 
	 * @param bytes The byte array which is filled with bytes read from the
	 *        buffer.
	 * 
	 * @return
	 * @throws IOException
	 */
	int get(byte[] bytes) throws IOException
	{
		int read = get(bytes, 0);

		while (read < bytes.length)
		{
			nextPage();
			read += get(bytes, read);
		}

		return read;
	}

	private void nextPage() throws IOException
	{
		super.pagePosition += super.pageSize;
		this.page = super.channel.map(MapMode.READ_ONLY, super.pagePosition, super.pageSize);
	}
}
